home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 60 / 60.xpi / chrome / webdeveloper.jar / content / webdeveloper / common / dom.js < prev    next >
Encoding:
JavaScript  |  2009-06-30  |  14.7 KB  |  525 lines

  1. // Returns the current content document
  2. function webdeveloper_getContentDocument()
  3. {
  4.     return webdeveloper_getSelectedBrowser().contentDocument;
  5. }
  6.  
  7. // Returns the current content window
  8. function webdeveloper_getContentWindow()
  9. {
  10.     return webdeveloper_getSelectedBrowser().contentWindow;
  11. }
  12.  
  13. // Returns the document body element
  14. function webdeveloper_getDocumentBodyElement(contentDocument)
  15. {
  16.     // If there is a body element
  17.     if(contentDocument.body)
  18.     {
  19.         return contentDocument.body;
  20.     }
  21.     else
  22.     {
  23.         var bodyElementList = contentDocument.getElementsByTagName("body");
  24.  
  25.         // If there is a body element
  26.         if(bodyElementList.length > 0)
  27.         {
  28.             return bodyElementList[0];
  29.         }
  30.     }
  31.  
  32.     return contentDocument.documentElement;
  33. }
  34.  
  35. // Returns the document head element
  36. function webdeveloper_getDocumentHeadElement(contentDocument)
  37. {
  38.     var headElementList = contentDocument.getElementsByTagName("head");
  39.  
  40.     // If there is a head element
  41.     if(headElementList.length > 0)
  42.     {
  43.         return headElementList[0];
  44.     }
  45.  
  46.     return contentDocument.documentElement;
  47. }
  48.  
  49. // Gets all the documents from the current page
  50. function webdeveloper_getDocuments(frame)
  51. {
  52.     var documents = new Array();
  53.  
  54.     // If the frame is set
  55.     if(frame)
  56.     {
  57.         var frames       = frame.frames;
  58.         var framesLength = frames.length;
  59.  
  60.         // If the frame document exists
  61.         if(frame.document)
  62.         {
  63.             documents.push(frame.document);
  64.         }
  65.  
  66.         // Loop through the frames
  67.         for(var i = 0; i < framesLength; i++)
  68.         {
  69.             documents = documents.concat(webdeveloper_getDocuments(frames[i]));
  70.         }
  71.     }
  72.  
  73.     return documents;
  74. }
  75.  
  76. // Get the ancestors of the element
  77. function webdeveloper_getElementAncestors(element)
  78. {
  79.     var ancestors = webdeveloper_getElementAncestorsInternal(element);
  80.  
  81.     // Reverse the list and remove the last element which is the original element
  82.     ancestors.reverse();
  83.     ancestors.pop();
  84.  
  85.     return ancestors;
  86. }
  87.  
  88. // Recursively gets the ancestors of an element
  89. function webdeveloper_getElementAncestorsInternal(element)
  90. {
  91.     var ancestors = new Array();
  92.  
  93.     // If the element is set
  94.     if(element)
  95.     {
  96.         var parentElement = element.parentNode;
  97.  
  98.         // If the element has a tag name
  99.         if(element.tagName)
  100.         {
  101.             ancestors.push(element);
  102.         }
  103.  
  104.         // If there is a parent element
  105.         if(parentElement)
  106.         {
  107.             ancestors = ancestors.concat(webdeveloper_getElementAncestorsInternal(parentElement));
  108.         }
  109.     }
  110.  
  111.     return ancestors;
  112. }
  113.  
  114. // Get the children of the element
  115. function webdeveloper_getElementChildren(element)
  116. {
  117.     var children = new Array();
  118.  
  119.     // If the element is set
  120.     if(element)
  121.     {
  122.         var child       = null;
  123.         var childNodes  = element.childNodes;
  124.         var childLength = childNodes.length;
  125.  
  126.         // Loop through the children
  127.         for(var i = 0; i < childLength; i++)
  128.         {
  129.             child = childNodes[i];
  130.  
  131.             // If the child and tag name are set
  132.             if(child && child.tagName)
  133.             {
  134.                 children.push(child);
  135.             }
  136.         }
  137.     }
  138.  
  139.     return children;
  140. }
  141.  
  142. // Get the position of the element
  143. function webdeveloper_getElementPosition(element, xPosition)
  144. {
  145.     var position = 0;
  146.  
  147.     // If the element is set
  148.     if(element)
  149.     {
  150.         var elementOffsetParent = element.offsetParent;
  151.  
  152.         // If the element has an offset parent
  153.         if(elementOffsetParent)
  154.         {
  155.             // While there is an offset parent
  156.             while((elementOffsetParent = element.offsetParent) != null)
  157.             {
  158.                 // If getting the x position
  159.                 if(xPosition)
  160.                 {
  161.                     position += element.offsetLeft;
  162.                 }
  163.                 else
  164.                 {
  165.                     position += element.offsetTop;
  166.                 }
  167.  
  168.                 element = elementOffsetParent;
  169.             }
  170.         }
  171.         else
  172.         {
  173.             // If getting the x position
  174.             if(xPosition)
  175.             {
  176.                 position = element.offsetLeft;
  177.             }
  178.             else
  179.             {
  180.                 position = element.offsetTop;
  181.             }
  182.         }
  183.     }
  184.  
  185.     return position;
  186. }
  187.  
  188. // Get the x position of the element
  189. function webdeveloper_getElementPositionX(element)
  190. {
  191.     return webdeveloper_getElementPosition(element, true);
  192. }
  193.  
  194. // Get the y position of the element
  195. function webdeveloper_getElementPositionY(element)
  196. {
  197.     return webdeveloper_getElementPosition(element, false);
  198. }
  199.  
  200. // Returns the text from an element
  201. function webdeveloper_getElementText(element)
  202. {
  203.     var elementText = "";
  204.  
  205.     // If the element is set
  206.     if(element)
  207.     {
  208.         var childNode       = null;
  209.         var childNodeList   = element.childNodes;
  210.         var childNodeLength = childNodeList.length;
  211.         var childNodeType   = null;
  212.  
  213.         // Loop through the child nodes
  214.         for(var i = 0; i < childNodeLength; i++)
  215.         {
  216.             childNode     = childNodeList[i];
  217.             childNodeType = childNode.nodeType;
  218.  
  219.             // If the child node type is an element
  220.             if(childNodeType == Node.ELEMENT_NODE)
  221.             {
  222.                 elementText += webdeveloper_getElementText(childNode);
  223.             }
  224.             else if(childNodeType == Node.TEXT_NODE)
  225.             {
  226.                 elementText += childNode.nodeValue + " ";
  227.             }
  228.         }
  229.     }
  230.  
  231.     return elementText;
  232. }
  233.  
  234. // Returns the list of the images for the specified document
  235. function webdeveloper_getImagesForDocument(contentDocument, includeBackgroundImages, includeIcons)
  236. {
  237.     var images = new Array();
  238.  
  239.     // If the content document is set
  240.     if(contentDocument)
  241.     {
  242.         var backgroundImage = null;
  243.         var computedStyle   = null;
  244.         var cssURI          = CSSPrimitiveValue.CSS_URI;
  245.         var documentURL     = contentDocument.documentURI;
  246.         var element         = null;
  247.         var image           = null;
  248.         var imageInterface  = Components.interfaces.nsIDOMHTMLImageElement;
  249.         var inputInterface  = Components.interfaces.nsIDOMHTMLInputElement;
  250.         var linkInterface   = Components.interfaces.nsIDOMHTMLLinkElement;
  251.         var treeWalker      = contentDocument.createTreeWalker(contentDocument, NodeFilter.SHOW_ELEMENT, null, false);
  252.         var url             = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(Components.interfaces.nsIURL);
  253.  
  254.         // While the tree walker has more nodes
  255.         while((element = treeWalker.nextNode()) != null)
  256.         {
  257.             // If this is an image element
  258.             if(element instanceof imageInterface)
  259.             {
  260.                 images.push(element);
  261.             }
  262.             else if(element instanceof inputInterface && element.src && element.type && element.type.toLowerCase() == "image")
  263.             {
  264.                 image     = new Image();
  265.                 url.spec  = documentURL;
  266.                 image.src = url.resolve(element.src);
  267.  
  268.                 // If this is not a chrome image
  269.                 if(image.src.indexOf("chrome://") != 0)
  270.                 {
  271.                     images.push(image);
  272.                 }
  273.             }
  274.             else if(includeIcons && element instanceof linkInterface && element.href && element.href.indexOf("chrome://") != 0 && element.rel && element.rel.indexOf("icon") != -1)
  275.             {
  276.                 image     = new Image();
  277.                 url.spec  = documentURL;
  278.                 image.src = url.resolve(element.href);
  279.  
  280.                 images.push(image);
  281.             }
  282.             else if(includeBackgroundImages)
  283.             {
  284.                 computedStyle = element.ownerDocument.defaultView.getComputedStyle(element, null);
  285.  
  286.                 // If the computed style is set
  287.                 if(computedStyle)
  288.                 {
  289.                     backgroundImage = computedStyle.getPropertyCSSValue("background-image");
  290.  
  291.                     // If this element has a background image and it is a URI
  292.                     if(backgroundImage && backgroundImage.primitiveType == cssURI)
  293.                     {
  294.                         image     = new Image();
  295.                         image.src = backgroundImage.getStringValue();
  296.  
  297.                         // If this is not a chrome image
  298.                         if(image.src.indexOf("chrome://") != 0)
  299.                         {
  300.                             images.push(image);
  301.                         }
  302.                     }
  303.                 }
  304.             }
  305.         }
  306.     }
  307.  
  308.     return images;
  309. }
  310.  
  311. // Returns the list of the objects for the specified document
  312. function webdeveloper_getObjectsForDocument(contentDocument)
  313. {
  314.     var objects = new Array();
  315.  
  316.     // If the content document is set
  317.     if(contentDocument)
  318.     {
  319.         var documentObjects       = contentDocument.embeds;
  320.         var documentObjectsLength = documentObjects.length;
  321.  
  322.         // Loop through the document objects
  323.         for(var i = 0; i < documentObjectsLength; i++)
  324.         {
  325.             objects.push(documentObjects[i]);
  326.         }
  327.     }
  328.  
  329.     return objects;
  330. }
  331.  
  332. // Returns the content document from a page load event
  333. function webdeveloper_getPageLoadEventContentDocument(event)
  334. {
  335.     // Try to get the event targets
  336.     try
  337.     {
  338.         var eventTarget    = event.target;
  339.         var originalTarget = event.originalTarget;
  340.     
  341.         // If the event targets are set and the original target is the document or the event target is the browser
  342.         if(eventTarget && originalTarget && (originalTarget.nodeName == "#document" || eventTarget == getBrowser()))
  343.         {
  344.             var contentDocument = eventTarget.contentDocument;
  345.  
  346.             // If the content document is not set and the original target default view parent is set
  347.             if(!contentDocument && originalTarget.defaultView && originalTarget.defaultView.parent)
  348.             {
  349.                 contentDocument = originalTarget.defaultView.parent.document;
  350.             }
  351.  
  352.             // If the content document is set and has the same URI as the original target
  353.             if(contentDocument && contentDocument.documentURI == originalTarget.documentURI)
  354.             {
  355.                 return contentDocument;
  356.             }
  357.         }
  358.     }
  359.     catch(exception)
  360.     {
  361.         // Do nothing
  362.     }
  363.     
  364.     return null;
  365. }
  366.  
  367. // Returns the selected browser
  368. function webdeveloper_getSelectedBrowser()
  369. {
  370.     return window.top.getBrowser().selectedBrowser;
  371. }
  372.  
  373. // Returns the list of the scripts for the specified document
  374. function webdeveloper_getScriptsForDocument(contentDocument, includeInline)
  375. {
  376.     var scripts = new Array();
  377.  
  378.     // If the content document is set
  379.     if(contentDocument)
  380.     {
  381.         var documentScript        = null;
  382.         var documentScripts       = contentDocument.getElementsByTagName("script");
  383.         var documentScriptsLength = documentScripts.length;
  384.         var documentURL           = contentDocument.documentURI;
  385.  
  386.         // Loop through the document scripts
  387.         for(var i = 0; i < documentScriptsLength; i++)
  388.         {
  389.             documentScript = documentScripts[i];
  390.  
  391.             // If including inline scripts or this is not inline
  392.             if(includeInline || (documentScript.src && documentScript.src != documentURL))
  393.             {
  394.                 scripts.push(documentScript);
  395.             }
  396.         }
  397.     }
  398.  
  399.     return scripts;
  400. }
  401.  
  402. // Inserts the given child after the element
  403. function webdeveloper_insertAfter(child, after)
  404. {
  405.     // If the child and after are set
  406.     if(child && after)
  407.     {
  408.         var nextSibling = after.nextSibling;
  409.         var parent      = after.parentNode;
  410.  
  411.         // If the element has a next sibling
  412.         if(nextSibling)
  413.         {
  414.             parent.insertBefore(child, nextSibling);
  415.         }
  416.         else
  417.         {
  418.             parent.appendChild(child);
  419.         }
  420.     }
  421. }
  422.  
  423. // Inserts the given child as the first child of the element
  424. function webdeveloper_insertAsFirstChild(element, child)
  425. {
  426.     // If the element and child are set
  427.     if(element && child)
  428.     {
  429.         // If the element has child nodes
  430.         if(element.hasChildNodes())
  431.         {
  432.             element.insertBefore(child, element.firstChild);
  433.         }
  434.         else
  435.         {
  436.             element.appendChild(child);
  437.         }
  438.     }
  439. }
  440.  
  441. // Returns true if the ancestor element is an ancestor of the element
  442. function webdeveloper_isAncestor(element, ancestorElement)
  443. {
  444.     // If the element and ancestor element are set
  445.     if(element && ancestorElement)
  446.     {
  447.         var parentElement = null;
  448.  
  449.         // Loop through the parent elements
  450.         while((parentElement = element.parentNode) != null)
  451.         {
  452.             // If the parent element is the ancestor element
  453.             if(parentElement == ancestorElement)
  454.             {
  455.                 return true;
  456.             }
  457.             else
  458.             {
  459.                 element = parentElement;
  460.             }
  461.         }
  462.     }
  463.  
  464.     return false;
  465. }
  466.  
  467. // Returns true if the page has frames
  468. function webdeveloper_pageHasFrames()
  469. {
  470.     // If the content document has a frame element
  471.     if(webdeveloper_getContentDocument().getElementsByTagName("frame").length > 0)
  472.     {
  473.         return true;
  474.     }
  475.  
  476.     return false;
  477. }
  478.  
  479. // Removes all child elements from an element
  480. function webdeveloper_removeAllChildElements(element)
  481. {
  482.     // If the element is set
  483.     if(element)
  484.     {
  485.         var childElements = element.childNodes;
  486.  
  487.         // Loop through the child elements
  488.         for(var i = 0; i < childElements.length; i++)
  489.         {
  490.             element.removeChild(childElements[i]);
  491.         }
  492.  
  493.         childElements = element.childNodes;
  494.  
  495.         // Loop through the child elements
  496.         while(childElements.length > 0)
  497.         {
  498.             element.removeChild(childElements[0]);
  499.         }
  500.     }
  501. }
  502.  
  503. // Removes all elements matching the XPath
  504. function webdeveloper_removeAllElementsByXPath(contentDocument, xPath)
  505. {
  506.     var elementList    = webdeveloper_evaluateXPath(contentDocument, xPath);
  507.     var elementsLength = elementList.length;
  508.  
  509.     // Loop through all the elements
  510.     for(var i = 0; i < elementsLength; i++)
  511.     {
  512.         webdeveloper_removeElement(elementList[i]);
  513.     }
  514.  
  515. }
  516.  
  517. // Removes an element
  518. function webdeveloper_removeElement(element)
  519. {
  520.     // If the element and it's parent node are set
  521.     if(element && element.parentNode)
  522.     {
  523.         element.parentNode.removeChild(element);
  524.     }
  525. }